
# This code is hosted on http://code.google.com/p/lenthorp/
# Freely available for use in applications, but should NOT be modified
# Email all comments to lenthorpresearch@gmail.com

import urllib
from datetime import datetime

def get_historical_dividends(symbol, start_date, end_date):
    if ';' in symbol:
        symbol = symbol.split(';')
        url = 'http://www.trustnet.com/Factsheets/Factsheet.aspx?fundCode=%s&univ=O&typeCode=%s&pagetype=dividends' % (symbol[0], symbol[1])
    else:
        url = 'http://www.trustnet.com/Factsheets/Factsheet.aspx?fundCode=%s&pagetype=dividends' % symbol

    dataTable = urllib.urlopen(url).read().strip()
    dataTable = dataTable[dataTable.index('dividend type'):]
    dataTable = dataTable[:dataTable.index('</table>')]
    dataTable = dataTable.replace('\r','')
    dataTable = dataTable.replace('\n','')
    dataTable = dataTable.replace('\t','')
    dataTable = dataTable.replace('</th>','')
    dataTable = dataTable.replace('</td>','')
    dataTable = dataTable.replace('</tr>','')
    dataTable = dataTable.replace(' style="text-align: center" class="altrow"','')
    dataTable = dataTable.replace(' style="text-align: center" class="odd"','')       
    dataTable = dataTable.split('<tr>')

    header = dataTable[0].split('<th>')
    header = [header[idx].strip() for idx in range(len(header))]
    dateIndex = header.index('Payment date')
    priceIndex = header.index('dividend amount')

    datas = dataTable[1:]
    returnData = []
    for data in datas:
        dataSplit = data.replace('<td>','',1)
        dataSplit = dataSplit.split('<td>')
        dataSplit = [dataSplit[idx].strip() for idx in range(len(dataSplit))]
        date = datetime.date(datetime.strptime(dataSplit[dateIndex],'%d-%b-%Y'))
        amount = float(dataSplit[priceIndex].replace('p','').strip())
        if date >= start_date and date <= end_date:
            returnData.append([date,amount])
        
    return returnData
    